﻿using CuocChienQueNha.Class.MapClass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace CuocChienQueNha.Class.Character.army
{
    public class Bullet
    {
        public bool isEnable { set; get; }
        int loai;
        float tocdo;
        Vector2 duongdi;
        Vector2 startPo;
        Vector2 stopPo;
        Texture2D image;
        Rectangle size;
        float satthuong;
        Vector2 curPo;
        Vector2 origin;
        float rotation;
        MapClass.Effect effect;
        int target;

        public Bullet(int loai, float tocdo, float satthuong, MapClass.Effect effect)
        {
            target = 0;
            isEnable = false;
            this.satthuong = satthuong;
            this.loai = loai;
            this.tocdo = tocdo;
            this.effect = effect;
        }
        public void setDuongdi(Vector2 ddau, Vector2 ddich, int target)
        {
            rotation = 0;
            isEnable = true;
            // tính đường đi
            #region
            if (Math.Abs(stopPo.X - startPo.X) > Math.Abs(stopPo.Y - startPo.Y))
            {
                duongdi = new Vector2(1, (stopPo.Y - startPo.Y) / (stopPo.X - startPo.X));
                if (stopPo.X < startPo.X) duongdi *= -1;
            }
            else
            {
                duongdi = new Vector2((stopPo.X - startPo.X) / (stopPo.Y - startPo.Y), 1);
                if (stopPo.Y < startPo.Y) duongdi *= -1;
            }
            #endregion


            //tính góc quay và độ lệch phù hợp 
            #region
            if (duongdi.X == 0)
            {
                if (duongdi.Y > 0)
                {
                    rotation = MathHelper.Pi;
                }
                else
                {
                    rotation = 0;
                }
            }
            else
            {
                if (duongdi.X > 0)
                {
                    rotation = (float)Math.Atan(duongdi.Y / duongdi.X) + MathHelper.Pi / 2;
                }
                else // hướng trái
                {
                    rotation = (float)Math.Atan(duongdi.Y / duongdi.X) - MathHelper.Pi / 2;
                }
            }
            #endregion

            curPo = startPo;

        }
        public void Load(ContentManager x)
        {
            image = x.Load<Texture2D>("Images/Character/Bullet/Bullet");
            size = new Rectangle((loai / 3) * 40, (loai * 3) * 40, 40, 40);
        }
        public void Draw(SpriteBatch spr)
        {
            origin = new Vector2(20, 20);
            spr.Draw(image, (curPo + origin), size, Color.White, rotation, origin, 0.5f, SpriteEffects.None, 0f);
        }
        public void Dichuyen(List<Enemy> enemy)
        {
            curPo += tocdo * duongdi;
            if ((target <= enemy.Count - 1) && enemy[target].isEnable)
            {
                if (enemy[target].isVaCham(curPo) || Vector2.Distance(stopPo, curPo) <= (tocdo / Math.Sqrt(2)))
                {
                    isEnable = false;
                    enemy[target].SubBlood(satthuong, effect);
                }
            }
            else
            {
                for (int i = 0; i < enemy.Count; i++)
                {
                    if (enemy[i].isVaCham(curPo))
                    {
                        isEnable = false;
                        enemy[i].SubBlood(satthuong, effect);
                        break;
                    }
                }
                if (Vector2.Distance(stopPo, curPo) <= (tocdo / Math.Sqrt(2))) isEnable = false;
            }
        }
    }
}
